<template>
  <Spin :spinning="loading" tip="Loading...">
  <div>
    <!-- 搜索 -->
    <!-- 预授权查询 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="120" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :fetchapi="fetchapi"
      :datakey="'records'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :isSelectColumn="false"
      columnsRef="AuthQueryDealerlgr"
    >
<!--      <template slot="controls" slot-scope="props">-->
<!--      <multiuse-button type="primary" :click="exportFileBtn.bind()">导出</multiuse-button>-->
<!--    </template>-->
    </FilterTable>
    <BaseDialog :visible.sync="visible" :title="'车型新增'" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <vehicle @close="close" @save="save"></vehicle>
    </BaseDialog>
    <Drawer :visible.sync="visibleEdit" :title="'预授权详情'" width="100%"  destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <editForm @close="editclose" @save="editsave" :rowData="rowData"></editForm>
    </Drawer>
  </div>
  </Spin>
</template>
<script>
  import vehicle from '@/pages/claimDemage/tools/ModelVehicle.vue';
  import { exportExcelFile } from '@/utils/index';
  import editForm from './editForm.vue';
  import { getAppId } from '@/assets/js/auth';
  import { getClaim } from '@/api/dictionary/index';
  import { getListByPage,getListForExport } from '@/api/claim/dealer/AuthQueryDealer';
  import { dictionary } from '@/utils/dictMixin';
  import moment from "moment";
  export default {
     mixins: [dictionary],
    components: {
      vehicle,
      editForm
    },
    data() {
      return {
        visible: false,
        visibleDrawer: false,
        collapse: true,
        visibleEdit: false,
        infoId: null,
        rowData: {},
        fetchapi: getListByPage,
        topFilterInit: this.topFilterInitFun(),
        flterValue: {}, //参数
        columns: this.creatHeader(),
        fetchParams: {authStatusStrs: '55031002, 55031003, 55031004, 55031005, 55031006',xhrAbort: true}, //ipa 接口
        selectedRows: [], //选中经销商
        dialogTitle: '选择车辆',
        visibleAdd: false,
        authNo: '' ,//预授权单号
        loading:false
      };
    },
    created() {
      if (getAppId() == 'arcfox') {
        this.topFilterInit.find(x => x.fieldName == 'ifComm').itemList =this.createDictList('5543',[55431001])
      }else{
        this.topFilterInit.find(x => x.fieldName == 'ifComm').itemList =this.createDictList('5543')
      }
      this.fetchParams.beginApplyAt = moment().startOf('week').format('YYYY-MM-DD HH:mm:ss');
      this.fetchParams.endApplyAt = moment().endOf('week').format('YYYY-MM-DD HH:mm:ss');
      this.chuqi();
      this.claimType()
    },
    methods: {
      //搜索数据
      topFilterInitFun() {
        return [
          { type: 'INPUT', label: '预授权单号', fieldName: 'authNo', placeholder: '请输入预授权单号...', initialValue: '' },
          // { type: 'INPUT', label: '维修工单号', fieldName: 'roNo', placeholder: '请输入', initialValue: '' },
          { type: 'SELECT', label: '预授权状态', fieldName: 'authStatus', placeholder: '请选择预授权状态...', initialValue: '', itemList: this.createDictList('5503',[55031001]), filterable: true },
          { type: 'INPUT', label: 'VIN', fieldName: 'vin', placeholder: '请输入vin...', initialValue: '' },
          { type: 'INPUT', label: '车牌号', fieldName: 'licenseNo', placeholder: '请输入车牌号...', initialValue: '' },
          {type: 'RANGE_DATE', label: '申请时间', fieldName: 'beginApplyAt|endApplyAt', placeholder: '请输入',initialValue: [
              moment()
                .startOf('week')
                .format('YYYY-MM-DD HH:mm:ss'),
              moment()
                .endOf('week')
                .format('YYYY-MM-DD HH:mm:ss')
            ] },
          // {type: 'INPUT', label: '发动机号/电机号', fieldName: 'engineNo', placeholder: '请输入发动机号/电机号...', initialValue: '' },
          {type: 'INPUT', label: '主损件名称', fieldName: 'partName', placeholder: '请输入主损件名称...', initialValue: '' },
          {type: 'INPUT', label: '主损件代码', fieldName: 'partCode', placeholder: '请输入主损件代码...', initialValue: '' },
          {type: 'SELECT', label: '预授权类型', fieldName: 'authType', placeholder: '请选择预授权类型...', initialValue: '', itemList:this.createDictList('5508',[]), filterable: true}
        ];
      },
      //搜索获取数据
      changeHandle(val) {
        if(!val.beginApplyAt) {
          this.$notify.warning({ title: '提示信息', message: '申请开始日期不可为空！' });
          return
        }
        if(!val.endApplyAt) {
          this.$notify.warning({ title: '提示信息', message: '申请结束日期不可为空！' });
          return
        }
        if(getAppId() == 'arcfox') {
          if (moment(moment(val.endApplyAt).format('YYYY-MM-DD')).diff(moment(val.beginApplyAt).format('YYYY-MM-DD'), 'months', true) > 12) {
            this.$notify.warning({ title: '提示信息', message: '申请日期跨度不可超过十二个月！' });
            return
          }
        } else {
          if (moment(moment(val.endApplyAt).format('YYYY-MM-DD')).diff(moment(val.beginApplyAt).format('YYYY-MM-DD'), 'months', true) > 12) {
            this.$notify.warning({ title: '提示信息', message: '申请日期跨度不可超过十二个月！' });
            return
          }
        }
        this.fetchParams = Object.assign({}, this.fetchParams, val,{authStatusStrs: '55031002, 55031003, 55031004, 55031005, 55031006' } ,{xhrAbort: false});
        // this.SELECT();
      },
      //table表头和数据源
      creatHeader() {
        return [
          {
            title: '序号',
            dataIndex: 'index',
            width: 80,
            sorter: true,
            fixed: 'left',
            render: props => {
              return <span>{props.row.index + 1}</span>;
            }
          },
          {
            title: '操作',
            dataIndex: 'column-action',
            width: 80,
            render: props => {
              return (
                //   <div style="text-align:center">
                //   <multiuse-button type="primary" icon="el-icon-edit" onClick={() => this.edit(props.row)} circle />
                // </div>
                <div style="text-align:center">
                  <multiuse-button size="mini" type="text" onClick={() => this.edit(props.row)}>
                    明细
                  </multiuse-button>
                </div>
              );
            }
          },
          { title: '预授权单号', dataIndex: 'authNo', sorter: true },
          { title: '预授权状态', dataIndex: 'statusStr', sorter: true },
          { title: '维修工单号', dataIndex: 'roNo', sorter: true },
          { title: 'VIN', dataIndex: 'vin', sorter: true },
          { title: '车牌号', dataIndex: 'licenseNo', sorter: true },
          { title: '车型名称', dataIndex: 'modelName', sorter: true },
          { title: '预授权类型', dataIndex: 'authTypeStr', sorter: true },
          { title: '常规预授权分类', dataIndex: 'authSubStr', sorter: true },
          { title: '主损件代码', dataIndex: 'partCode', sorter: true },
          { title: '主损件名称', dataIndex: 'partName', sorter: true },
          { title: '供应商', dataIndex: 'supName', sorter: true },
          { title: '行驶里程', dataIndex: 'driveMile', sorter: true },
          // { title: '发动机号/电机号', dataIndex: 'engineNo', sorter: true },
          { title: '故障现象代码', dataIndex: 'faultCode', sorter: true },
          { title: '故障现象名称', dataIndex: 'faultName', sorter: true },
          { title: '故障原因代码', dataIndex: 'frCode', sorter: true },
          { title: '故障原因名称', dataIndex: 'frName', sorter: true },
          // { title: '驳回次数', dataIndex: 'channelNum', sorter: true },
          { title: '费用合计', dataIndex: 'sumAmount', sorter: true },
          { title: '工时费', dataIndex: 'labourAmount', sorter: true },
          { title: '备件费', dataIndex: 'partAmount', sorter: true },
          { title: '附加费用', dataIndex: 'addAmount', sorter: true },
          { title: '保修起始日期', dataIndex: 'repairBeginDate', sorter: true , dateFormat: 'yyyy-MM-dd'},
          { title: '保修截止日期', dataIndex: 'repairEndDate', sorter: true , dateFormat: 'yyyy-MM-dd'},
          // { title: '生产日期', dataIndex: 'prodDate', sorter: true , dateFormat: 'yyyy-MM-dd'},
          { title: '销售日期', dataIndex: 'saleDate', sorter: true, dateFormat: 'yyyy-MM-dd' },
          { title: '开单日期', dataIndex: 'serviceBeginDate', sorter: true, dateFormat: 'yyyy-MM-dd' },
          { title: '申请人', dataIndex: 'applyBy', sorter: true },
          { title: '申请时间', dataIndex: 'applyAt', sorter: true, width: 140 },
          { title: '审核人', dataIndex: 'checkBy', sorter: true },
          { title: '审核时间', dataIndex: 'checkDate', sorter: true }
          // { title: '主键', dataIndex: 'authId', sorter: true, hidden: true }
        ];
      },
      //选中
      selectChange(rows) {
        this.selectedRows = rows;
        console.log(this.selectedRows);
      },  // 导出
    async  exportFileBtn() {

        let beginApplyAt = this.topFilterInit.find(x => x.fieldName == "beginApplyAt|endApplyAt").initialValue[0];
        let endApplyAt = this.topFilterInit.find(x => x.fieldName == "beginApplyAt|endApplyAt").initialValue[1];
        if(!beginApplyAt) {
          this.$notify.warning({ title: '提示信息', message: '申请开始日期不可为空！' });
          return
        }
        if(!endApplyAt) {
          this.$notify.warning({ title: '提示信息', message: '申请结束日期不可为空！' });
          return
        }
        if(getAppId() == 'arcfox') {
          if (moment(moment(endApplyAt).format('YYYY-MM-DD')).diff(moment(beginApplyAt).format('YYYY-MM-DD'), 'months', true) > 12) {
            this.$notify.warning({ title: '提示信息', message: '申请日期跨度不可超过十二个月！' });
            return
          }
        } else {
          if (moment(moment(endApplyAt).format('YYYY-MM-DD')).diff(moment(beginApplyAt).format('YYYY-MM-DD'), 'months', true) > 12) {
            this.$notify.warning({ title: '提示信息', message: '申请日期跨度不可超过十二个月！' });
            return
          }
        }
        let obj = Object.assign({}, this.fetchParams,{authStatusStrs: '55031002, 55031003, 55031004, 55031005, 55031006' } ,{xhrAbort: false});
        obj.beginApplyAt = this.topFilterInit.find(x => x.fieldName == "beginApplyAt|endApplyAt").initialValue[0];
        obj.endApplyAt = this.topFilterInit.find(x => x.fieldName == "beginApplyAt|endApplyAt").initialValue[1];
        this.loading=true;
        getListForExport(obj).then(res => {
          exportExcelFile(res.data, '预授权查询导出');
          this.loading=false;
        });
      },
      async chuqi() {
        // const res = await getPartType({ type: 5503 });
        // //草稿
        // delete res.data[0];
        // delete res.data[5];
        // if (res.resultCode == 200) {
        //   this.topFilterInit.find(x => x.fieldName == 'authStatus').initialValue = '';
        //   this.topFilterInit.find(x => x.fieldName == 'authStatus').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
        // }
        // const res4 = await getPartType( {type:5543});
        // if (res4.resultCode == 200) {
        //
        //   if (getAppId() == 'arcfox') {
        //     delete res4.data[0];
        //   }
        //
        //   this.topFilterInit.find(x => x.fieldName == 'ifComm').initialValue = '';
        //   this.topFilterInit.find(x => x.fieldName == 'ifComm').itemList = res4.data.map(y =>({ text: y.codeCnDesc, value: y.codeId }));
        // }
      },
      async claimType() {
        // const res = await getclaimType({ type: 5501 });
        // if (res.resultCode == 200) {
        //   this.topFilterInit.find(x => x.fieldName == 'cmsClaimType').initialValue = '';
        //   this.topFilterInit.find(x => x.fieldName == 'cmsClaimType').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
        // }
        const res2 = await getClaim();
        if (res2.resultCode == 200) {
          delete res2.data[7];
          this.topFilterInit.find(x => x.fieldName == 'cmsClaimType').initialValue = '';
          this.topFilterInit.find(x => x.fieldName == 'cmsClaimType').itemList = res2.data.map(y => ({ text: y.name, value: y.code }));
        }
      },
      close() {
        this.visible = false;
      },
      chooseOrderNo() {
        this.visible = true;
      },
      save(obj) {
        this.topFilterInit.find(x => x.fieldName == 'modelCode').initialValue = obj[0].modelCode;
        this.visible = false;
      },
      editclose() {
        this.visibleEdit = false;
        // this.fetchParams = { ...this.fetchParams };
      },
      edit(val) {
        this.rowData = val;
        this.visibleEdit = true;
      },
      editsave() {
        this.visibleEdit = false;
       // this.fetchParams = { ...this.fetchParams };
      }
    }
    // mounted() {
    //   this.BaseTable = this.$refs.listModal.$refs.pageTable;
    // }
  };
</script>

<style lang="less" scoped></style>
